from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os
import undetected_chromedriver as uc
# 获取扩展的绝对路径
extension_dir = os.path.abspath('proxy')

# 创建一个临时的扩展文件 (.crx 不是必须的，加载解压的扩展目录即可)
chrome_options = Options()
chrome_options.add_argument(f'--load-extension={extension_dir}')

# 可选：为了安静地运行，可以禁用一些提示
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# chrome_options.add_argument('--headless') # 如果需要无头模式

print('xxxxx')
driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
driver = uc.Chrome(driver_executable_path=driver_path,options=chrome_options)
driver.set_window_size(1366,768)
# 给扩展一点时间加载和设置代理
time.sleep(3)

try:
    # 验证代理
    driver.get('https://httpbin.org/ip')
    time.sleep(10)


finally:
    driver.quit()
